package com.nulldev.util.graphics.renderIt.api.utils;

import com.nulldev.util.graphics.renderIt.api.renderItGraphicsInterface;
import com.nulldev.util.graphics.renderIt.renderer.global.rColors;

public class RITDemoTri {

	final int x[] =
		{ 100, 70, 130 };
	final int y[] =
		{ 50, 100, 100 };

	private double vertex[][][][] =
		{
				{
						{
								{ -1, -1, -1 },
								{ 1, -1, -1 } },
						{
								{ -1, 1, -1 },
								{ 1, 1, -1 } } },
				{
						{
								{ -1, -1, 1 },
								{ 1, -1, 1 } },
						{
								{ -1, 1, 1 },
								{ 1, 1, 1 } } } };

	// rotations in radians
	private double xyR = 0.005, xzR = 0.005, yzR = 0.005;

	// view attributes
	private int SCALE = 200, OFFSET = 350, DIAMETER = 12;

	public RITDemoTri() {
	}

	// apply plane rotation to 3D point, only 2 coordinates are affected
	final void xyRotate(double p[], double sin, double cos) {
		double temp;
		temp = cos * p[0] + sin * p[1];
		p[1] = -sin * p[0] + cos * p[1];
		p[0] = temp;
	}

	final void xzRotate(double p[], double sin, double cos) {
		double temp;
		temp = cos * p[0] + sin * p[2];
		p[2] = -sin * p[0] + cos * p[2];
		p[0] = temp;
	}

	final void yzRotate(double p[], double sin, double cos) {
		double temp;
		temp = cos * p[1] + sin * p[2];
		p[2] = -sin * p[1] + cos * p[2];
		p[1] = temp;
	}

	public void draw(final renderItGraphicsInterface rgi) {
		for (int x = 0; x < 2; x++) {
			for (int y = 0; y < 2; y++) {
				for (int z = 0; z < 2; z++) {
					xyRotate(vertex[x][y][z], Math.sin(xyR), Math.cos(xyR));
					xzRotate(vertex[x][y][z], Math.sin(xzR), Math.cos(xzR));
					yzRotate(vertex[x][y][z], Math.sin(yzR), Math.cos(yzR));
				}
			}
		}

		// draw cube edges
		for (int x = 0; x < 2; x++) {
			for (int y = 0; y < 2; y++) {
				rgi.setColor(rColors.red);
				drawEdge(vertex[x][y][0][0], vertex[x][y][0][1], vertex[x][y][1][0], vertex[x][y][1][1], rgi);
				rgi.setColor(rColors.blue);
				drawEdge(vertex[x][0][y][0], vertex[x][0][y][1], vertex[x][1][y][0], vertex[x][1][y][1], rgi);
				rgi.setColor(rColors.green);
				drawEdge(vertex[0][x][y][0], vertex[0][x][y][1], vertex[1][x][y][0], vertex[1][x][y][1], rgi);
			}
		}

		// draw cube vertices
		for (int x = 0; x < 2; x++) {
			for (int y = 0; y < 2; y++) {
				for (int z = 0; z < 2; z++) {
					drawVertex(vertex[x][y][z][0], vertex[x][y][z][1], rgi);
				}
			}
		}
	}

	final void drawEdge(double x1, double y1, double x2, double y2, final renderItGraphicsInterface g) {
		g.bd_drawLine((int) (x1 * SCALE) + OFFSET, (int) (-y1 * SCALE) + OFFSET, (int) (x2 * SCALE) + OFFSET, (int) (-y2 * SCALE) + OFFSET);
	}

	final void drawVertex(double x, double y, final renderItGraphicsInterface g) {
		g.setColor(rColors.yellow);
		g.bd_fillOval((int) (SCALE * x) + OFFSET - DIAMETER / 2, (int) (SCALE * (-y)) + OFFSET - DIAMETER / 2, DIAMETER, DIAMETER);

		g.setColor(rColors.black);
		g.bd_drawOval((int) (SCALE * x) + OFFSET - DIAMETER / 2, (int) (SCALE * (-y)) + OFFSET - DIAMETER / 2, DIAMETER, DIAMETER);
	}
}
